﻿Imports System.Collections
Imports System.Collections.Hashtable
Imports System.Data.SqlClient
Imports System.Data

Public Class Form1
    Public SQLCONNECTION As SqlConnection
    Public STRCONNECT As String

    Public SQLCOMMAND1 As SqlCommand
    Public SQLCOMMAND2 As SqlCommand

    Public SQLDATAREADER1 As SqlDataReader
    Public SQLDATAREADER2 As SqlDataReader

    Public HashTabl As Hashtable
    Public ListObjt As List(Of TUNIT)

    Public HashData As Hashtable


    'Public Function Xnull(ByVal item As Object) As String
    '    If item Is Nothing Or item.Equals(System.DBNull.Value) Then
    '        Return ""
    '    Else
    '        Return Trim(CType(item, String))
    '    End If
    'End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        STRCONNECT = "Data Source=.;Initial Catalog=DBZH_TAX;User ID=sa;Password=sa"
        SQLCONNECTION = New SqlConnection(STRCONNECT)
        SQLCONNECTION.Open()


        SQLCOMMAND1 = New SqlCommand("SELECT  * FROM TBL_DW where dw_type='建筑安装业'", SQLCONNECTION)
        SQLDATAREADER1 = SQLCOMMAND1.ExecuteReader

        'ListObjt = TUNIT.GetListObjt(SQLDATAREADER1)
        HashTabl = TUNIT.GetHashTabl(SQLDATAREADER1)
        System.Console.WriteLine(HashTabl.Count)
        SQLDATAREADER1.Close()


        SQLCOMMAND2 = New SqlCommand("select sum(JE) as je,kjnd,kjqj,dw_scode from taxdata a where exists (select *  from tbl_dw b where b.dw_type='建筑安装业' and a.dw_scode=b.dw_scode and kjnd=2013 and kjqj=1) group by kjnd,kjqj,dw_scode", SQLCONNECTION)
        SQLDATAREADER2 = SQLCOMMAND2.ExecuteReader
        HashData = TDATA.GetHashTabl(SQLDATAREADER2)
        SQLDATAREADER2.Close()

        'BEGIN READ DATA
        'HashTabl = New HashTable()
        'Do While SQLDATAREADER.Read
        '    Dim TEMP As String
        '    Dim UNIT As TUNIT = New TUNIT()


        '    If Not SQLDATAREADER.IsDBNull(SQLDATAREADER.GetOrdinal("DW_ZCDZ")) Then
        '        TEMP = SQLDATAREADER.GetString(SQLDATAREADER.GetOrdinal("DW_ZCDZ"))
        '        System.Console.WriteLine(TEMP)
        '    End If

        '    UNIT.UNITCODE = SQLDATAREADER.GetString(SQLDATAREADER.GetOrdinal("DW_SCode"))
        '    UNIT.UNITNAME = SQLDATAREADER.GetString(SQLDATAREADER.GetOrdinal("DW_Name"))
        '    'UNIT.UNITTYPE = SQLDATAREADER.GetString(SQLDATAREADER.GetOrdinal("DW_TYPE"))
        '    'UNIT.UNITADDR = Xnull(SQLDATAREADER.GetString(SQLDATAREADER.GetOrdinal("DW_ZCDZ")))

        '    HashTabl.Add(UNIT.UNITCODE, UNIT)
        'Loop



    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        'If IsNothing(HashTabl) Then
        '    MessageBox.Show("请先读取数据.")
        '    Exit Sub
        'Else
        '    MessageBox.Show("查找:2321088141219479")
        'End If

        'Dim UnitX As TUNIT

        'If HashTabl.ContainsKey("2321088141219479") Then
        '    UnitX = HashTabl("2321088141219479")
        '    If Not IsNothing(UnitX) Then
        '        MessageBox.Show(UnitX.UNITNAME)
        '    End If
        'End If


    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'If IsNothing(ListObjt) Then
        '    MessageBox.Show("请先读取数据.")
        '    Exit Sub
        'End If

        'shift hashtabl to list

        ListObjt = TUNIT.HashToList(HashTabl)
        DataGridView1.DataSource = ListObjt
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim de As DictionaryEntry
        Dim data As TDATA
        Dim unit As TUNIT
        For Each de In HashData
            data = de.Value
            System.Console.WriteLine(data.UNITCODE)

            If HashTabl.ContainsKey(data.UNITCODE) Then
                unit = HashTabl(data.UNITCODE)
                unit.DATA201301 = data.DATAMONY
            End If
        Next
    End Sub
End Class
